package com.eyas.business.controllor.role;

import com.eyas.business.model.jdbcEntity.table.RoleInfo;
import com.eyas.business.model.pojo.roleservice.MenuIndexDTO;
import com.eyas.business.model.pojo.roleservice.MenuTreeTableDTO;
import com.eyas.business.service.role.RoleService;
import com.eyas.core.model.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: 王龙龙
 * @Date: 2019/2/26 15:43
 * @Description:
 */
@Controller
@RequestMapping("/eyas/role/")
public class RoleAction {
    @Autowired
    private RoleService roleService;

    @GetMapping("/queryMenuInfos")
    @ResponseBody
    public List<MenuIndexDTO> queryMenuInfos(HttpSession session) {
        List<MenuIndexDTO> menuIndexDTO_list = roleService.queryMenuInfos(session);
        return menuIndexDTO_list;
    }

    @GetMapping("/queryRoleInfo")
    @ResponseBody
    public PageResult<RoleInfo> queryRoleInfo(String rolename, String validflag, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        PageResult<RoleInfo> pageResult = this.roleService.queryRoleInfo(rolename,validflag,page-1,limit);
        return pageResult;
    }

    @PutMapping("/saveRoleInfo")
    @ResponseBody
    public String saveRoleInfo(RoleInfo roleInfo) {
        this.roleService.saveRoleInfo(roleInfo);
        return "{\"success\":true}";
    }

    @PutMapping("/roleinfo/{roleid}/validflag")
    @ResponseBody
    public String updateRoleInfoValidflag(@PathVariable int roleid) {
        this.roleService.updateRoleInfoValidflag(roleid);
        return "{\"success\":true}";
    }

    @GetMapping("/edit")
    public String edit(int roleid, Model model) {
        RoleInfo roleInfo = this.roleService.getRoleInfoByID(roleid);
        model.addAttribute("roleinfo",roleInfo);
        return "/roleinfo/edit";
    }

    @GetMapping("/authedit")
    public String authedit(int roleid, Model model) {
        model.addAttribute("roleid",roleid);
        return "/roleinfo/authedit";
    }

    @GetMapping("/queryMenusByRoleId")
    @ResponseBody
    public PageResult<MenuTreeTableDTO> queryMenusByRoleId(int roleid) {
        PageResult<MenuTreeTableDTO> pageResult = this.roleService.queryMenusByRoleId(roleid);
        return pageResult;
    }

    @PutMapping("/changeMenuRole")
    @ResponseBody
    public String changeMenuRole(int roleid,int menuid,boolean checkflag) {
        this.roleService.changeMenuRole(roleid,menuid,checkflag);
        return "{\"success\":true}";
    }
}
